package com.srwl.mytx.adapter;

import android.content.Context;
import android.graphics.RectF;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bm.library.Info;
import com.bm.library.PhotoView;
import com.github.florent37.viewanimator.ViewAnimator;
import com.srwl.mytx.Constant;
import com.srwl.mytx.R;
import com.srwl.mytx.domain.AliOssService;
import com.srwl.mytx.domain.MyFile;
import com.srwl.mytx.utils.GlideLoadUtils;

import java.util.List;

import cn.jzvd.JzvdStd;


public class BigPictureAdapterV2 extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = "BigPictureAdapter";
    private final Context context;
    private final int screenHeight;
    private final int screenWidth;
    private List<MyFile> myFileList;
    private InteractionListener listener;
    private Info info;//小图位置信息
    private boolean fromInfoUsed;

    public BigPictureAdapterV2(Context context, List<MyFile> myFileList, InteractionListener listener) {
        this.context = context;
        this.myFileList = myFileList;
        this.listener = listener;
        screenHeight = context.getResources().getDisplayMetrics().heightPixels;
        screenWidth = context.getResources().getDisplayMetrics().widthPixels;
    }

    public void showAnimateOut(Info smallImageViewInfo, PhotoView photoView, int position) {
//        photoView.animaTo(smallImageViewInfo, new Runnable() {
//            @Override
//            public void run() {
//                reset();
//            }
//        });
        Info bigImageViewInfo = PhotoView.getBigImageViewInfo(photoView);
        RectF bigImageRectF = bigImageViewInfo.getmImgRect();

        RectF smallRectF = smallImageViewInfo.getmImgRect();
        smallRectF.width();
        float scaleX = smallRectF.width() / bigImageRectF.width();
        float scaleY = smallRectF.height() / bigImageRectF.height();
        float translationY = bigImageViewInfo.getmRect().centerY() - smallImageViewInfo.getmRect().centerY();
        float translationX = bigImageViewInfo.getmRect().centerX() - smallImageViewInfo.getmRect().centerX();

//        Log.d(TAG, "showAnimateOut:translationY :" + translationY);
//        Log.d(TAG, "showAnimateOut:translationX :" + translationX);
//        Log.d(TAG, "showAnimateOut:currentView.getTranslationX():" + photoView.getTranslationX());
//        Log.d(TAG, "showAnimateOut:currentView.getTranslationY():" + photoView.getTranslationY());
        ViewAnimator.animate(photoView).scaleX(scaleX).scaleY(scaleY).translationY(0, -translationY).translationX(0, -translationX).duration(400).start();
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == Constant.MY_FILE_TYPE_PICTURE) {
            return new PictureViewHolder(LayoutInflater.from(context).inflate(R.layout.tx_item_big_picture, parent, false));
        } else {
            return new PictureViewHolder(LayoutInflater.from(context).inflate(R.layout.tx_video_player, parent, false));
        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        holder.setIsRecyclable(false);
        if (holder instanceof PictureViewHolder) {
            PictureViewHolder viewHolder = (PictureViewHolder) holder;
            //开启缩放功能
            viewHolder.photoView.enable();
            viewHolder.photoView.setAnimaDuring(400);
            //判断展示的文件是否为本地文件还是远程文件
            String loadUrl;
            MyFile myFile = myFileList.get(position);
            if (myFile.getStatus() == MyFile.FILE_STATUS_REMOTE) {
                loadUrl = AliOssService.TOPIC_IMAGE_PRE_URL + myFile.getUrl();
            } else {
                loadUrl = myFile.getUrl();
            }
            //Glide.with(context).asBitmap().load(loadUrl).into(viewHolder.photoView);
            GlideLoadUtils.glideLoad(context,loadUrl,viewHolder.photoView);

            if (info != null && !fromInfoUsed) {
                fromInfoUsed = true;
                viewHolder.photoView.animaFrom(info);
            }
            viewHolder.photoView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    viewHolder.photoView.setOnClickListener(null);
                    Info info = listener.onGetImageRectInfo(position);
                    viewHolder.tvCount.setVisibility(View.INVISIBLE);
                    if (info != null) {
                        showAnimateOut(info, viewHolder.photoView, position);
                    } else {
                        Log.e(TAG, "onClick: 获取小图消息失败 position：" + position);
                    }
                }
            });

        }
    }

    @Override
    public int getItemCount() {
        return myFileList.size();
    }

    @Override
    public int getItemViewType(int position) {
        return myFileList.get(position).getType();
    }

    class PictureViewHolder extends RecyclerView.ViewHolder {
        private PhotoView photoView;
        private TextView tvCount;
        private ImageView iv_back;

        public PictureViewHolder(@NonNull View itemView) {
            super(itemView);
            photoView = itemView.findViewById(R.id.iv_picture);
            tvCount = itemView.findViewById(R.id.tv_picture_count);
            iv_back = itemView.findViewById(R.id.iv_back);
        }
    }

    class VideoViewHolder extends RecyclerView.ViewHolder {
        private JzvdStd videoPlayer;
        private TextView tvCount;
        private ImageView iv_back;

        public VideoViewHolder(@NonNull View itemView) {
            super(itemView);
            videoPlayer = itemView.findViewById(R.id.video_player);
            tvCount = itemView.findViewById(R.id.tv_picture_count);
            iv_back = itemView.findViewById(R.id.iv_back);
        }
    }

    public void setInfo(Info info) {
        this.info = info;
        fromInfoUsed = false;
    }

    public interface InteractionListener {

        void onItemClick(int position);

        void onItemRemove(int position);

        Info onGetImageRectInfo(int position);
    }
}
